/*******************************************************************************
 * FilePath    : /Src/BSP/INT.c
 * Version     : 1.0
 * Brief       :
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-04-16 14:38:38
 * LastEditors : AndrewHu
 * LastEditTime: 2022-02-16 16:48:27
 * Description :
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile unsigned int SysTick_Count = 0;
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 * @param  None
 * @retval None
 */
void NMI_Handler(void)
{
  RCC->CIR |= 0x1 << 23;
}

/**
 * @brief  This function handles Hard Fault exception.
 * @param  None
 * @retval None
 */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles SVCall exception.
 * @param  None
 * @retval None
 */
void SVC_Handler(void)
{
}

/**
 * @brief  This function handles PendSVC exception.
 * @param  None
 * @retval None
 */
void PendSV_Handler(void)
{
}
/*******************************************************************************
 * Function    : GetSysTickCount
 * Brief       : 获取滴答时间
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 16:49:39
 * Description :
 ******************************************************************************/
u32 GetSysTickCount(void) // porting api
{
  return SysTick_Count;
}
/*******************************************************************************
 * Function    : Delay_ms
 * Brief       : 延时函数
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-17 14:39:33
 * Description : 单位-毫秒，在该函数内不退出，计时完成后退出
 ******************************************************************************/
void Delay_ms(u8 delay)
{
  u32 temp = SysTick_Count;
  while ((SysTick_Count - temp) < delay)
  {
    IWDG_ReloadCounter(); //清狗
  }
}
/**
 * @brief  This function handles SysTick Handler.
 * @param  None
 * @retval None
 */
void SysTick_Handler(void)
{
  SysTick_Count++;
  ble_nMsRoutine();
}

void EXTI0_1_IRQHandler(void)
{
  EXTI_ClearITPendingBit(EXTI_Line0);
}

void IRQ_HANDLER(void)
{
  EXTI_ClearITPendingBit(IRQ_EXTI);

  if (GetRunningStatus() == RunningMode_Stop)
  { // stop
    RCC->CR |= RCC_CR_HSION;
    RCC->CR |= RCC_CR_PLLON;
    RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
    SysTick_Config(48000);
  }
  SetRunningMode(RunningMode_Normal);

  ble_run(0);
}

void UART1_IRQHandler(void) //串口1中断服务程序
{
  if (UART_GetITStatus(UART1, UART_IT_RXIEN) != RESET) //接收中断
  {
    UART_ClearITPendingBit(UART1, UART_IT_RXIEN);
    UartRx_Int();
  }
  if (UART_GetITStatus(UART1, UART_IT_TXIEN) != RESET)
  {
    UART_ClearITPendingBit(UART1, UART_IT_TXIEN);
    UartTx_Int();
  }
}
